
#include "gpt.h"

/*===================== GLOBAL VARIABLES DECLARATION ============================*/
uint32_t led;
uint32_t led1;
Gpt_ConfigType Gpt_Status;

#pragma DATA_SEG SHARED_DATA
	tCallbackFunction GPT_PIT_ISR_Callback[8];
#pragma DATA_SEG DEFAULT

/*======================== FUNCTION DEFINITIONS =================================*/

/*********************************************************************************
*	Name: Gpt_Init    
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: Enables Gpt module for the right register parameters
**********************************************************************************/

void Gpt_Init( const Gpt_ConfigType* ConfigPtr ){
  
	/* Pointers to the Configurations registers of the MC*/
	const Gpt_ChannelConfigType * ChannelCfg;
	uint8_t channel_counter;
	unsigned int * __far pitld = &PITLD0; 
	/* visual validation of th led*/
	led = 0;
	led1= 0;
	/* PIT counter freeze while in Freeze mode */
  PITCFLMT_PITFRZ        = 1u;
	/*Clear Interruption Flags*/
	PITTF = (uint8_t)0xFF; /*Clear all Flags*/
	/*if the configurations parameters are null we will tacke our own configuration parameters*/
	if(NULL == ConfigPtr){
		/* The reference for the configuration is the internal configurations structure*/
		Gpt_Status = Gpt_Config;
	}
	else{
		/* If the reference is not NULL the value for the configurations willl be the ConfigPtr*/
		Gpt_Status = *ConfigPtr;
	}

	for(channel_counter =0; channel_counter < Gpt_Status.NumChannels_pit; channel_counter++){
		ChannelCfg = &Gpt_Status.ChannelConfigs_pit[channel_counter];
		/* Enabling Channel */
		PITCE |= (uint8_t)0x01<<ChannelCfg->Channel_ID;
		/* Seting Callback */
		GPT_PIT_ISR_Callback[channel_counter]=ChannelCfg->Channel_Callback;
		/* Interruption Enable */
		if(NOTIFICATION_ENABLE == ChannelCfg->notification_status)
		{
			PITINTE |= (uint8_t)0x01<<ChannelCfg->Channel_ID;
  	}
		/* formula to calculate PIT value ( ( CNF_MICROTIMER_PERIODX / PIT_TARGET_FREQ ) - 1 )*/
		if(MICROTIMER_0 == ChannelCfg->microtimer_select)
		{
			PITMUX &= (uint8_t)~(0x01<<ChannelCfg->Channel_ID);
			pitld[ChannelCfg->Channel_ID] = (uint16_t)( CNF_MICROTIMER_PERIOD0 / ChannelCfg->target_frequency )-1;
		}
		if(MICROTIMER_1 == ChannelCfg->microtimer_select)
		{
			PITMUX |= (uint8_t)(0x01<<ChannelCfg->Channel_ID);
			pitld[ChannelCfg->Channel_ID] = (uint16_t)( CNF_MICROTIMER_PERIOD1 / ChannelCfg->target_frequency )-1;
		}
		GPT_PIT_ISR_Callback[ChannelCfg->Channel_ID] = ChannelCfg->Channel_Callback;
	}
  /*First we load predefined configurations por de Micro timers*/	
	PITMTLD0 = CNF_GPT_MICROTIMER_DIV0;
	PITMTLD1 = CNF_GPT_MICROTIMER_DIV1;
   INT_CFADDR             = 0x70; 	/* with vectors from 0xFF70 to 0xFF7E */ 
   INT_CFDATA5        = 0x06; 	/* Periodic Interrupt Timer Channel 0, S12 Core handled, priority 6 */
}

void Gpt_StartTimer( Gpt_ChannelType Channel, Gpt_ValueType Value ){
	/* Load 8-bit microtimer load register 0 into the 8-bit micro timer down-counter 0 */
	PITCFLMT_PFLMT1 	= 1u;
  PITCFLMT_PFLMT0     = 1u;
  /* Load 16-bit timer load register 0 into the 16-bit timer down-counter 0 */
  PITFLT = (uint8_t)0x01<<Channel;
  /* Enabling PIT */
  PITCFLMT_PITE       = 1u;
}

void Gpt_StopTimer( Gpt_ChannelType Channel ){
	/*Enabling Channel*/
	PITCE &= (uint8_t)~(0x01<<Channel);
}

void Gpt_EnableNotification( Gpt_ChannelType Channel ){
	/* Enable Channel interrupt in PITINTE*/
	PITINTE |= (uint8_t)0x01<<Channel;
}

void Gpt_DisableNotification( Gpt_ChannelType Channel ){
	/* Disable Channel interrupt in PITINTE*/
	PITINTE &= (uint8_t)~(0x01<<Channel);
}

void Gpt_SetChannelCallback( Gpt_ChannelType Channel, tCallbackFunction Callback){

	GPT_PIT_ISR_Callback[Channel] = Callback;
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED

void interrupt  Gpt_Channel0_Isr( void  )
{

    GPT_PIT_ISR_Callback[0]();
    /* Clear the real time interrupt flag */
    PITTF_PTF0 = 1u;
}
#pragma CODE_SEG DEFAULT

#pragma CODE_SEG __NEAR_SEG NON_BANKED

void interrupt  Gpt_Channel1_Isr( void  )
{

	//GPT_PIT_ISR_Callback[1]();
    /* Clear the real time interrupt flag */
    PITTF_PTF1 = 1u;
}
#pragma CODE_SEG DEFAULT
/*******************************************************************************/
